# test_new_key.py
import requests
import json

# 新的API密钥
API_KEY = "sk-456e421beddf49f2966de60790fa37fe"

def test_new_api_key():
    """测试新API密钥的各项服务"""
    
    print("=" * 60)
    print("🔑 测试新API密钥")
    print("=" * 60)
    print(f"密钥: {API_KEY[:10]}...{API_KEY[-10:]}")
    print()
    
    # 测试1: 基础文本生成
    print("【测试1】基础文本生成 (qwen-turbo)")
    print("-" * 60)
    
    try:
        response = requests.post(
            "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation",
            headers={
                "Authorization": f"Bearer {API_KEY}",
                "Content-Type": "application/json"
            },
            json={
                "model": "qwen-turbo",
                "input": {
                    "prompt": "你好,请用一句话介绍你自己"
                },
                "parameters": {
                    "result_format": "text"
                }
            },
            timeout=10
        )
        
        print(f"状态码: {response.status_code}")
        
        if response.status_code == 200:
            result = response.json()
            print("✅ 文本生成服务正常!")
            print(f"响应: {json.dumps(result, ensure_ascii=False, indent=2)[:300]}")
        elif response.status_code == 401:
            print("❌ 密钥无效! 请检查:")
            print("   1. 密钥是否正确复制")
            print("   2. 密钥是否已被删除")
            print(f"   错误详情: {response.text}")
        elif response.status_code == 403:
            print("⚠️ 权限不足! 需要开通服务")
            print("   前往: https://dashscope.console.aliyun.com/")
            print(f"   错误详情: {response.text}")
        else:
            print(f"⚠️ 未知错误 ({response.status_code})")
            print(f"   响应: {response.text}")
            
    except requests.exceptions.Timeout:
        print("❌ 请求超时,请检查网络连接")
    except Exception as e:
        print(f"❌ 请求失败: {e}")
    
    print()
    
    # 测试2: 图像生成
    print("【测试2】图像生成服务 (wanx-v1)")
    print("-" * 60)
    
    try:
        response = requests.post(
            "https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis",
            headers={
                "Authorization": f"Bearer {API_KEY}",
                "Content-Type": "application/json",
                "X-DashScope-Async": "enable"
            },
            json={
                "model": "wanx-v1",
                "input": {
                    "prompt": "一只可爱的小猫在草地上玩耍"
                },
                "parameters": {
                    "size": "1024*1024",
                    "n": 1
                }
            },
            timeout=10
        )
        
        print(f"状态码: {response.status_code}")
        
        if response.status_code in [200, 202]:
            print("✅ 图像生成服务正常!")
            result = response.json()
            print(f"响应: {json.dumps(result, ensure_ascii=False, indent=2)[:300]}")
        elif response.status_code == 403:
            print("⚠️ 图像生成服务未开通")
            print("   需要在控制台开通「通义万相」服务")
        else:
            print(f"⚠️ 状态码: {response.status_code}")
            print(f"   响应: {response.text[:300]}")
            
    except Exception as e:
        print(f"❌ 请求失败: {e}")
    
    print()
    
    # 测试3: 语音合成
    print("【测试3】语音合成服务 (sambert)")
    print("-" * 60)
    
    try:
        response = requests.post(
            "https://dashscope.aliyuncs.com/api/v1/services/aigc/text2speech/speech-synthesis",
            headers={
                "Authorization": f"Bearer {API_KEY}",
                "Content-Type": "application/json"
            },
            json={
                "model": "sambert-zhichu-v1",
                "input": {
                    "text": "测试语音合成"
                },
                "parameters": {
                    "voice": "zhixiaobai",
                    "format": "mp3"
                }
            },
            timeout=10
        )
        
        print(f"状态码: {response.status_code}")
        
        if response.status_code == 200:
            print("✅ 语音合成服务正常!")
        elif response.status_code == 403:
            print("⚠️ 语音合成服务未开通")
            print("   需要在控制台开通语音合成服务")
        else:
            print(f"⚠️ 状态码: {response.status_code}")
            print(f"   响应: {response.text[:300]}")
            
    except Exception as e:
        print(f"❌ 请求失败: {e}")
    
    print()
    print("=" * 60)
    print("测试完成!")
    print("=" * 60)

if __name__ == "__main__":
    test_new_api_key()